#include <Unit.h>
#include <iostream>

using namespace std;
Unit::Unit(const UNIT_TYPE& type, const int factorOfBaseUnit):type(type), factorOfBaseUnit(factorOfBaseUnit)
{
}

int Unit::getAmountInBaseUnit(const int amount) const
{
  return amount * factorOfBaseUnit;
}

bool Unit::isSameType(const Unit& unit)  const
{
  return &type == &(unit.type);
}

void Unit::printOneUnit(ostringstream& os,int& amountLeftInBaseUnit) const
{
   if (int amountInThisUnit = amountLeftInBaseUnit/factorOfBaseUnit)
   {	             
        if (os.str().size())
        {
            os<<" ";
        }
        os << amountInThisUnit << " "<< getUnitString();
        amountLeftInBaseUnit = amountLeftInBaseUnit % factorOfBaseUnit;
   }
}


const std::string Unit::getAndPrint(const PrintFormat& format, int amount) const
{
   cout << amount << " " << getUnitString()<<" = ";
   
   int amountLeftInBaseUnit = getAmountInBaseUnit(amount);

   string printString = printEveryUnit(format,amountLeftInBaseUnit);
       
   if (printString.size() == 0 )
   {
        printString += "0 ";
        printString += getBaseUnit().getUnitString();
   }
	
   cout<< printString  <<endl;

   return printString;
}



#define INVALID_AMOUNT  (0)


const UNIT_TYPE Invalid_type;


InvalidUnit::InvalidUnit(const int factorOfBaseUnit): Unit(Invalid_type, factorOfBaseUnit)
{
    
}

const Unit& InvalidUnit::getBaseUnit() const
{  
  return Invalid();
}

const InvalidUnit& InvalidUnit::Invalid()
{
  static InvalidUnit unit(INVALID_AMOUNT);
  return unit;
}


